package thread;

//有三个线程，分别只能打印A，B和C
//要求按顺序打印ABC，打印10次
public class demo8 {

    public static void main(String[] args) {
        Object object1 = new Object();
        Object object2 = new Object();
        Object object3 = new Object();
        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    System.out.print("A");
                        synchronized (object2) {
                            object2.notify();
                        }
                        synchronized (object1) {
                            object1.wait();
                        }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (object2) {
                        object2.wait();
                    }
                    System.out.print("B");
                    synchronized (object3) {
                        object3.notify();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (object3) {
                        object3.wait();
                    }
                    System.out.println("C");
                    synchronized (object1) {
                        object1.notify();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t2.start();
        t3.start();
        t1.start();
    }
}
